package com.atldy.redis.controller;

import cn.hutool.core.util.IdUtil;
import com.atldy.redis.pojo.User;
import com.atldy.redis.pojo.dto.UserAddDTO;
import com.atldy.redis.pojo.vo.UserVO;
import com.atldy.redis.service.UserService;
import com.github.javafaker.Faker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h2> 用户前段控制器 </h2>
 *
 * @Author: li-dong-yang
 * @CreateTime: 2021-11-09 0:20
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户接口服务", tags = "用户接口服务")
public class UserControoler {

    @Autowired
    private UserService userService;

    Boolean result = false;

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @GetMapping("/addUser")
    public Boolean addUser() {


        Faker faker = new Faker();
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername(faker.name().name());
            user.setPassword(IdUtil.randomUUID());
            user.setSex(1);
            user.setIsDeleted(0);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            users.add(user);
        }

        users.parallelStream().forEach(user -> {
            UserAddDTO dto = new UserAddDTO();
            BeanUtils.copyProperties(user, dto);
            result = userService.addUser(dto);
        });
        return result;
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @GetMapping("/deletedUser")
    public Boolean deletedUser(Integer id) {
        return userService.deletedUser(id);
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping("/updateUser")
    public Boolean updateUser(@RequestBody UserAddDTO dto) {
        return userService.updateUser(dto);
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/getUserById")
    public UserVO getUserById(Integer id) {
        return userService.getUserById(id);
    }

}